<template>
  <div class="login" @keyup.enter="aaa">
    <div class="centerbox">
      <div class="bg">
        <img class="img1" src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="" prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            type="password"
            v-model="form.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="onSubmit('form')">登录</el-button>
          <el-button class="btncz" @click="cz">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login_api } from "@/api";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456"
      },
      rules: {
        username: [
          //  { validator: validatePass, trigger: 'blur' }
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    aaa() {
      console.log(1);
    },
    onSubmit(form) {
      /*   this.$refs[form].validate(valid => {
        console.log(valid);
        if (valid) {
          this.$axios.post("/login", this.form).then(res => {
            console.log(res);
            //登录成功跳转至home  否则弹出提示
            if (res.data.meta.status == "200") {
              this.$message("登录成功");
              this.$router.push("/home");
            } else {
              this.$message.error("用户名或密码错误");
            }
          });
        }
      }); */
      //封装
      this.$refs[form].validate(async valid => {
        if (valid) {
          //如果规则通过  发送请求
          // console.log(valid);
          const res = await login_api(this.form);
          // const res1 = await login_api(this.form1);

          if (res.data.meta.status == 200) {
            this.$message("登录成功");

            window.sessionStorage.setItem("token", res.data.data.token);
            this.$router.push("/home"); //跳转主页面
          } else {
            this.$message.error("用户名或密码错误");
          }
        }
      });
    },
    cz() {
      this.form.username = "";
      this.form.password = "";
    }
  }
};
</script>
<style scoped lang="less">
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .centerbox {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .el-form {
      margin-top: 120px;
      padding: 0 20px;
    }
    .btn {
      text-align-last: right;
    }
    .bg {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      position: absolute;
      border: 10px solid #fff;
      background-color: #eeeeee;
      left: 50%;
      top: -25%;
      transform: translate(-50%);
      overflow: hidden;
      box-shadow: 0 0 10px #ddd;
    }
    .img1 {
      width: 100%;
      height: 100%;
    }
    .btncz {
      background-color: #909399;
      color: #fff;
    }
  }
}
</style>
